package com.cw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.entity.LJysqdxxXmmx;
import com.cw.entity.LJytmxx;
import com.cw.mapper3.LJytmxxMapper;
import com.cw.service.IExamineSpecialService;
import com.cw.service.ILJytmxxService;
import com.cw.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author ly
 * @since 2021-08-16
 */
@Service
public class LJytmxxServiceImpl extends ServiceImpl<LJytmxxMapper, LJytmxx> implements ILJytmxxService {

    @Autowired
    private IExamineSpecialService iExamineSpecialService;

    /**
     * 获取检验是否完成
     * @param ljysqdxxXmmxs 检验明细表
     * @param clinicCode 处方号
     * @return java.util.Map<java.lang.String,java.util.List<java.lang.String>
     */
    @Override
    public Map<String, List<String>> getLjytmxxByExaminaimCode(List<LJysqdxxXmmx> ljysqdxxXmmxs,String clinicCode) {
        if (CollectionUtil.isEmpty(ljysqdxxXmmxs)) { return null; }


        LambdaQueryWrapper<LJytmxx> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LJytmxx::getPatientid, ljysqdxxXmmxs.get(0).getPatientId());
        lambdaQueryWrapper.eq(LJytmxx::getPatientno, clinicCode);
        List<LJytmxx> list = list(lambdaQueryWrapper);

        List<LJytmxx> lJytmxxes = new ArrayList<>();
        //遍历循环是否有组套检验,如果有就给他细拆出来
        for (LJytmxx lJytmxx : list) {
            String examinaimcode = lJytmxx.getExaminaimcode();

            int index;
            index = examinaimcode.indexOf("^");
            //没找到^直接下一次循环,这里不放心数据来源还是写一个吧以防万一Bug了
            if (index == -1) { continue; }
            //第二次查找是否有^
            index = examinaimcode.indexOf("^", index+1);
            if(index == -1){ lJytmxxes.add(lJytmxx); continue; }

            String[] split = examinaimcode.split("\\^");
            for (String s : split) {
                LJytmxx newLJytmxx = new LJytmxx();
                BeanUtil.copyProperties(lJytmxx,newLJytmxx);
                newLJytmxx.setExaminaimcode(s);
                lJytmxxes.add(newLJytmxx);
            }

        }
//        Map<String, LJytmxx> lJytmxxMap = lJytmxxes.stream().collect(Collectors.toMap(LJytmxx::getExaminaimcode, t->t));
        Map<String, LJytmxx> lJytmxxMap =new HashMap<>();
        for (LJytmxx item :lJytmxxes)
        {
            if(!lJytmxxMap.containsKey(item.getExaminaimcode()))
            {
                lJytmxxMap.put(String.join("-",item.getDoctrequestno(), item.getExaminaimcode()) ,item);
            }
        }
        if (CollectionUtil.isEmpty(lJytmxxMap)) { return null; }
        //清理字符串中的^符号
        lJytmxxMap = cleanTheString(lJytmxxMap);

        //返回的结果集
        Map<String, List<String>> resultMap = new HashMap<>();

        List<String> doIt = new ArrayList<>();
        List<String> notDoIt = new ArrayList<>();

        for (LJysqdxxXmmx ljysqdxxXmmx : ljysqdxxXmmxs) {
            Integer ztid = ljysqdxxXmmx.getZtid();
            if (ztid == null) { continue; }

            String stringVersion =String.join("-",ljysqdxxXmmx.getDoctrequestno(),String.valueOf(ztid)) ;
            boolean checkOut = lJytmxxMap.containsKey(stringVersion);
            if (!checkOut) { notDoIt.add(ljysqdxxXmmx.getYlmc()); continue; }
            LJytmxx lJytmxx = lJytmxxMap.get(stringVersion);
            doIt.add(lJytmxx.getExaminaim());
        }

        resultMap.put("doIt", doIt);
        resultMap.put("notDoIt", notDoIt);
        return resultMap;
    }

    @Override
    public List<LJytmxx> getExaminaimCodeByDoctrequestno(List<String> doctrequestnoList) {
        if (CollectionUtil.isEmpty(doctrequestnoList)) { return null; }
        LambdaQueryWrapper<LJytmxx> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(LJytmxx::getDoctrequestno, doctrequestnoList);
        List<LJytmxx> list = list(lambdaQueryWrapper);
        return list;
    }

    /**
     * 清楚字符串中的^符号
     * @param examinaimCode 需要清理的集合
     * @return java.Util.List<java.lang.String>
     */
    private List<String> cleanTheString(List<String> examinaimCode) {
        List<String> strings = new ArrayList<>();
        for (String code : examinaimCode) {
            if(StringUtil.isEmpty(code)){ continue; }
            String coded = code;
            while (true) {
                int index = coded.indexOf("^");
                if (index == -1) {
                    break;
                }
                String head = coded.substring(0, index);
                String back = coded.substring(index+1);
                coded = head + back;
            }
            strings.add(coded);
        }
        return strings;
    }

    /**
     * 清楚字符串中的^符号
     * @param examinaimCode 需要清理的集合
     */
    private Map<String, LJytmxx> cleanTheString(Map<String, LJytmxx> examinaimCode) {
        Map<String, LJytmxx> resultMap = new HashMap<>();
        Set<String> codes = examinaimCode.keySet();

        for (String code : codes) {
            if(StringUtil.isEmpty(code)){ continue; }
            String coded = code;
            while (true) {
                int index = coded.indexOf("^");
                if (index == -1) {
                    break;
                }
                String head = coded.substring(0, index);
                String back = coded.substring(index+1);
                coded = head + back;
            }
            resultMap.put(coded,examinaimCode.get(code));
        }
        return resultMap;
    }

}
